package com.ycq.server;

import com.ycq.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(value = "/",description = "这是我全部的post请求")
public class MyPostmethod {
    //变量装cookies
    private static Cookie cookie;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "登录接口，成功后获得cookies",httpMethod = "post")
    public String login(HttpServletResponse response,
                        @RequestParam(value = "userName",required = true) String userName,
                        @RequestParam(value = "passWord",required = true) String passWord){

        if (userName.equals("zhangsan")&&passWord.equals("12")){
             cookie=new Cookie("login","enen");
             response.addCookie(cookie);
             return "登录成功";
        }
        return "用户名或密码错误";

    }

    @RequestMapping(value = "/getUserList",method = RequestMethod.POST)
    @ApiOperation(value = "获取用户列表",httpMethod = "post")
    public String getUserList(HttpServletRequest request,
                            @RequestBody User user){
        User user1;
        //获取cookies
        Cookie[] cookies= request.getCookies();
        //验证cookies合法
        for (Cookie cookie1:cookies){
            if (cookie1.getName().equals("login")&&cookie1.getValue().equals("enen")){
                user=new User();
                user.setName("fanfan");
                user.setAge("18");
                return  user.toString();
            }
        }
        return "参数不合法";
    }


    }
